//1、头文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/device.h>
struct stm32mp157{
	unsigned int major;
	struct class *cls;
	struct device *dev;
};
struct stm32mp157 *drv_hello;

int drv_hello_open(struct inode *ide, struct file *filp)
{
	int ret =0;
	printk("---%s----\r\n",__FUNCTION__);
	return ret;
}

struct file_operations hello_fops={
	.open = drv_hello_open,
};


//2、加载函数
static int __init drv_hello_init(void)
{
	int ret =0;
	printk("---%s---\r\n",__FUNCTION__);
	drv_hello = kzalloc(sizeof(struct stm32mp157), GFP_KERNEL);
	if(IS_ERR(drv_hello))
	{
		printk("kzalloc_err\r\n");
		ret = PTR_ERR(drv_hello);
		return ret;
	}
	drv_hello->major = register_chrdev(0, "drv_hello", &hello_fops);
	if(drv_hello->major<0)
	{
		printk("register_chrdev_err\r\n");
		ret = drv_hello->major;
		goto register_chrdev_err;
	}
	drv_hello->cls = class_create(THIS_MODULE, "drv_hello");
	if(IS_ERR(drv_hello->cls))
	{
		printk("class_create_err\r\n");
		ret = PTR_ERR(drv_hello->cls);
		goto class_create_err;
	}
	drv_hello->dev = device_create(drv_hello->cls, NULL, MKDEV(drv_hello->major, 1), NULL, "drv_hello");
	if(IS_ERR(drv_hello->dev))
	{
		printk("device_create_err\r\n");
		ret = PTR_ERR(drv_hello->dev);
		goto device_create_err;
	}
	return ret;
device_create_err:
	class_destroy(drv_hello->cls);
class_create_err:
	unregister_chrdev(drv_hello->major, "drv_hello");
register_chrdev_err:
	kfree(drv_hello);
	return ret;
}

//3、卸载函数
void __exit drv_hello_exit(void)
{
	printk("---%s---\r\n",__FUNCTION__);
	device_destroy(drv_hello->cls, MKDEV(drv_hello->major, 1));
	class_destroy(drv_hello->cls);
	unregister_chrdev(drv_hello->major, "drv_hello");
	kfree(drv_hello);
}

//4、模块声明与认证
module_init(drv_hello_init);
module_exit(drv_hello_exit);
MODULE_LICENSE("GPL");



